package test

import (
	"gin-demo/common"
	"gin-demo/container"
	dao "gin-demo/dao/test"
	"gin-demo/entity"
)

type TestServiceImp struct {
}

func (t *TestServiceImp) List(pageRequest common.PageRequest) (*entity.TestList, error) {

	var testDao = container.Get("testDao").(dao.TestDaoImp)
	tests, err := testDao.List(pageRequest)
	if err != nil {
		return nil, err
	}
	for _, test2 := range tests.Tests {
		cover2Age(test2)
	}
	return tests, nil
}

func (t *TestServiceImp) Create(test *entity.Test) (*entity.Test, error) {

	var testDao = container.Get("testDao").(dao.TestDaoImp)
	test, err := testDao.Create(test)
	if err != nil {
		return nil, err
	}
	cover2Age(test)
	return test, nil
}

func cover2Age(test2 *entity.Test) {

	if test2.Age < 18 {
		test2.ShowAge = "未成年"
	}
	if test2.Age > 18 {
		test2.ShowAge = "成年"
	}
}
